<!-- eslint-disable no-undef -->
<!-- eslint-disable vue/multi-word-component-names -->
// eslint-disable-next-line vue/multi-word-component-names
<!-- eslint-disable vue/no-unused-vars -->
<!-- eslint-disable vue/multi-word-component-names -->
<template>
  <div>
    <search-bar @onSearch="onSearch"></search-bar>
    <el-button @click="showAddModal" type="primary" style="margin:15px 0">新增商品</el-button>
    <el-table row-key="id" :data="cates" style="width: 100%">
      <el-table-column prop="cateName" label="分类名" width="180" />
      <el-table-column prop="cateIcon" label="分类图标" width="180">
        <template #default="{ row }">
          <div>
            <img :src="row.cateIcon" width="50" alt="">
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="enable" label="是否启用">
        <template #default="{ row }">
          <div>
            <el-switch v-model="row.enable" inline-prompt active-text="启用" @change="switchEnable(row.id, row.enable)" inactive-text="未启用"/>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button type="primary" @click="showUpdateModal(row.id)">编辑</el-button>
          <el-button type="danger" @click="delCate(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      v-model="addModalShow"
      title="新增商品"
      width="40%"
    >
    <cate-add :cates="cates"></cate-add>
    </el-dialog>
    <el-dialog
      v-model="updateModalShow"
      title="修改商品"
      width="40%"
    >
    <cate-update></cate-update>
    </el-dialog>
  </div>
</template>

<script>
import { fetchCates, updateCate, delCate } from '@a/cateLists'
import SearchBar from '../../components/SearchBar.vue'
import CateAdd from './components/CateAdd.vue'
import CateUpdate from './components/CateUpdate.vue'
export default {
  name: 'CateLists',
  data () {
    return {
      cateId: -1,
      cates: [],
      searchParams: {
        key: '',
        start: '',
        end: ''
      },
      addModalShow: false,
      updateModalShow: false
    }
  },
  methods: {
    delCate (id) {
      this.$confirm('此操作会彻底删除分类，继续吗？', '删除').then(() => {
        delCate(id).then(res => {
          if (res.data.code === 200) {
            this.$message({
              type: 'success',
              message: '删除成功'
            })
            this.fetchCates()
          }
        })
      })
    },
    showUpdateModal (id) {
      this.updateModalShow = true
      this.cateId = id
    },
    showAddModal () {
      this.addModalShow = true
    },
    onSearch (searchParams) {
      this.searchParams = searchParams
      this.fetchCates()
    },
    switchEnable (id, enable) {
      console.log(id, enable)
      updateCate({
        id,
        enable
      }).then(res => {
        if (res.data.code === 200) {
          this.$message({
            type: 'success',
            message: '修改分类状态成功'
          })
          this.fetchCates()
        }
      })
    },
    fetchCates () {
      fetchCates({
        ...this.searchParams
      }).then(res => {
        if (res.data.code === 200) {
          this.cates = res.data.data
        }
      })
    }
  },
  created () {
    this.fetchCates()
  },
  components: {
    SearchBar,
    CateAdd,
    CateUpdate
  }
}
</script>

<style lang="scss" scoped></style>
